sap.ui.define([
    "sap/ui/core/Control",
], function(
	Control
) {
	"use strict";

	return Control.extend("firorworkshop185.control.SortTable", {
        metadata: {
            aggregations: {
                _table: { type: "sap.m.Table", multiple: false, visibility: "hidden" },
            },
        },

        onBeforeRendering() {
            this.initTable();
        },

        initTable() {
            let list = Object.keys(this.getModel("info").oData.order);
            var oTable = new sap.m.Table({
                // selectionChange: this.selectionChange.bind(this),
            });
            oTable.setMode("MultiSelect");

            var oColumn = new sap.m.Column({
                header: new sap.m.Text({
                    text: ""
                })
            });

            oTable.addColumn(oColumn);

            let count = 0;
            list.forEach(element => {
                var oItem = new sap.m.ColumnListItem({
                    cells: [
                        // new sap.m.Text({ text: this.getModel("info").oData.order[element].text }),
                        new sap.m.Text({ text: element }),
                    ]
                });
                oTable.addItem(oItem);
                if (this.getModel("info").oData.order[element].ifSort) {
                    oTable.setSelectedItem(oItem);
                    count++;
                }
            });
            this.setAggregation("_table", oTable);
        },

        closeSortSettingDialog() {
            var oTable = this.getAggregation("_table");

            let choosedList = [];
            oTable.getSelectedItems().forEach(element => {
                choosedList.push(element.mAggregations.cells[0].mProperties.text);
            });
            let allList = Object.keys(this.getModel("info").oData.order);

            var oModel = this.getModel('info');
			let Obj = oModel.oData.order;
            allList.forEach(element => {
                if (choosedList.indexOf(element) != -1) {
                    Obj[element].ifSort = true;
                } else {
                    Obj[element].ifSort = false;
                }
            });
			oModel.setData({
				...oModel.oData,
				order: Obj
			});
        },

        renderer(oRm, oControl) {
            // 此处的参数onControl指的就是该Control组件
            oRm.openStart("div", oControl);
            // 创建一个div
            oRm.openEnd();
            oRm.renderControl(oControl.getAggregation("_table"));
            oRm.close("div");
            // 闭合div
        }
	});
});